import MTEnv from './MTEnv.js'
import Dynamic from './dynamic.js'
import Role from './role.js'

import mapSettings from './mapSettings.js'

class MapItem {
  /*
    index 索引值
    options 设定
    parent 地图对象指针
  */
  constructor (index, options = {}, parent = null) {
    this.rowIndex = options.rowIndex
    this.colIndex = options.colIndex
    this.parentMap = parent
    this.objects = []

    this.initObjects(options)
  }

  // 初始化该地图区块的对象
  initObjects (options = {}) {
    const objects = options.objects
    const len = objects.length

    this.objects = []

    for (let i = 0; i < len; i++) {
      if (objects[i]) {
        let obj = null
        if (objects[i].type === 'Tile' || objects[i].type === 'Autotile') {
          // 都是基础的环境对象
          obj = new MTEnv(objects[i], this)
        }
        else if (object[i].type === 'Dynamic') {
          // 游戏场景中的互动对象
          obj = new Dynamic(objects[i], this)
        }
        else if (objects[i].type === 'Role') {
          // 游戏场景中的角色对象
          obj = new Role(objects[i], this)
        }

        if (obj) {
          this.objects.push(obj)
        }
      }
    }
  }

  // 初始化区块资源，使其可以显示出来
  async initSrc () {
    const len = this.objects.length
    for (let i = 0; i < len; i++) {
      const obj = this.objects[i]
      await obj.initSrc()
    }
  }
}

export default MapItem